﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core.Logger;

namespace Cofe.Core
{
    public class EventBuffer<T>
        where T : EventArgs
    {
        #region Constructor

        public EventBuffer()
        {
            _events = new Queue<IEventLog<T>>();
        }

        #endregion

        #region Methods

        public void Add(IEventLog<T> eventLog)
        {
            _events.Enqueue(eventLog);
        }

        public IEnumerable<IEventLog<T>> Take(int batchSize)
        {
            int count = _events.Count;
            int size = count < batchSize ? count : batchSize;

            Queue<IEventLog<T>> results = new Queue<IEventLog<T>>(size);

            for (int i = 0; i < size; i++)
            {
                results.Enqueue(_events.Dequeue());
            }
            return results;
        }

        
        
        #endregion

        #region Data

        private Queue<IEventLog<T>> _events;

        #endregion

        #region Public Properties

        public bool IsEmpty { get { return !_events.Any(); } }
        public int Size { get { return _events.Count; } }
        
        #endregion

        
    }
}
